package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.MbrMemberScore;
import com.macro.mall.model.MbrMemberScoreStatistics;
import com.macro.mall.service.MbrMemberScoreService;
import com.macro.mall.utils.FilePortUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @author super
 * @title: CommonController
 * @description: TODO
 * @date 2020/8/15 14:16
 */
@Slf4j
@RestController
@RequestMapping("score")
@Api(tags = "score", description = "评分表(score)")
public class MbrMemberScoreController {

    @Autowired
    private MbrMemberScoreService mbrMemberScoreService;


    @ApiOperation("查询评价")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<MbrMemberScore> list(@ApiParam("1-课程、2-视频、3-文章、4-服务订单、5-视频订单、6-讲师") @RequestParam(value = "targetType") Integer targetType,
                                             @ApiParam("如果是课程，对应的是课程id,如果是视频，对应的是视频ID") @RequestParam(value = "targetId") String targetId
    ) {
        MbrMemberScore one = mbrMemberScoreService.findOne(targetType, targetId);
        return CommonResult.success(one);
    }

    @ApiOperation("查询专题平均分(只针对课程)")
    @RequestMapping(value = "/listAvg", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult listAvg(@ApiParam("专题ID") @RequestParam(value = "trainingId") Integer trainingId) {
        return CommonResult.success(mbrMemberScoreService.selectTrainingAvg(trainingId));
    }

    @ApiOperation("添加评分")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@RequestBody MbrMemberScore request) {
        try {
            mbrMemberScoreService.saveNotNull(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("专题评论最多的标签")
    @RequestMapping(value = "/tagMax", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult scoreMax(@RequestParam(value = "commonType") String commonType,
                                 @RequestParam(value = "trainingId") Integer trainingId,
                                 @RequestParam(value = "courseId",required = false) Integer courseId,
                                 @RequestParam(value = "num") Integer num
    ) {
        return CommonResult.success(mbrMemberScoreService.scoreMax(commonType,trainingId,courseId,num));
    }

    @ApiOperation("查询评价列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult listAll(@ApiParam("评价人") @RequestParam(value = "name",required = false) String username,
                                @ApiParam("手机号") @RequestParam(value = "phone",required = false) String phone,
                                @ApiParam("1-课程、2-视频、3-文章、4-服务订单、5-视频订单、6-讲师") @RequestParam(value = "targetType",required = false) Integer targetType,
                                @ApiParam("社区名称") @RequestParam(value = "communityName",required = false) String communityName,
                                @ApiParam("课程名称") @RequestParam(value = "courseName",required = false) String courseName,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(mbrMemberScoreService.findAll(username,phone,targetType,communityName,courseName,pageNum,pageSize)));
    }

    @ApiOperation(value = "评价结果导出")
    @RequestMapping(value = "/exportResultList", method = RequestMethod.GET)
    @ResponseBody
    public void  exportSignResultList(@ApiParam("评价人") @RequestParam(value = "name",required = false) String username,
                                      @ApiParam("手机号") @RequestParam(value = "phone",required = false) String phone,
                                      @ApiParam("1-课程、2-视频、3-文章、4-服务订单、5-视频订单、6-讲师") @RequestParam(value = "targetType",required = false) Integer targetType,
                                      @ApiParam("社区名称") @RequestParam(value = "communityName",required = false) String communityName,
                                      @ApiParam("课程名称") @RequestParam(value = "courseName",required = false) String courseName,
                                      @RequestParam(value = "pageSize", defaultValue = "10000") Integer pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      HttpServletResponse response) {
        try {
            //导出的表名
            String title = "评价结果";
            //表中第一行表头字段
            String[] headers = {"姓名","手机号","社区","身份信息","专题","课程","评价对象","星级","标签","文字内容","评价时间"};
            //实际数据结果集
            List<MbrMemberScoreStatistics> resultList = mbrMemberScoreService.findAll(username,phone,targetType,communityName,courseName,pageNum,pageSize);
            //具体需要写入excel需要哪些字段，这些字段取自UserReward类，也就是上面的实际数据结果集的泛型
            List<String> listColumn = Arrays.asList("name","phone","communityName","userIdentityName","trainingName","courseName","targetName","score","label","commentContent","createTime");
            try {
                FilePortUtil.exportExcel1(response, title, headers, resultList, listColumn);
//                mbrMemberScoreService.exportExcelScore(response, title, headers, resultList, listColumn);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }catch (Exception e) {
            log.error(e.getMessage());
        }
    }

}
